// components/aquariumMes.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        exists:'',
        aquariumMess: {},
        tip: '',
        value1: Number,
        value2: Number,
        value3: Number,
        value4: Number,

        referenceV1: "6.5 ~ 8.5",
        referenceV2: "20 ~ 28",
        referenceV3: "0.5 ~ 0.7",
        referenceV4: "20 ~ 40",

        fishNum: Number,
        maxNum: Number,
        gmtCreate: '',
        charge: '',
        phone: '',
        edit: false,
        // 用来存储进步器的值，初始值跟maxNum相等，当确认修改成功后：tempMax的值为修改后的值；确认修改失败后，tempMax的值为maxNum
        tempMax:Number,


    },

    /**
     * 组件的方法列表
     */
    methods: {

        addAquarium(){
            wx.redirectTo({
              url: '../../pages/index/index?selectIndex=4',
            })
        },

        maxChange(e) {
            console.log(e.detail)
            if (e.detail < this.data.fishNum || e.detail > 999) {
                wx.showToast({
                    title: '超出限制！',
                    icon: 'error',
                    duration: 2000
                })
                return
            }

            this.setData({
                tempMax: e.detail
            })
            console.log(this.data.tempMax)
        },

        overlimit() {
            wx.showToast({
                title: '超出限制！',
                icon: 'error',
                duration: 2000
            })
        },

        editMax() {

            if (!this.data.edit) {
                this.setData({
                    edit: !this.data.edit
                })
                return
            }


            if (this.data.tempMax < this.data.fishNum || this.data.tempMax > 999) {
                wx.showToast({
                    title: '超出限制！',
                    icon: 'error',
                    duration: 2000
                })
                return
            }
            // 如果是确认,发送请求更新最大容量
            //todo：发送请求修改最大容量
            

            let aquariumId = wx.getStorageSync('userAquariumList')[wx.getStorageSync('curIndex')];

            let maxNum = this.data.tempMax;

            console.log(aquariumId)
            console.log("请求要修改成：")
            console.log(this.data.tempMax)

            wx.request({
              url: `http://120.77.156.205:8808/wx/aquarium/editMaxFishNum/${aquariumId}/${maxNum}`,
              method:'GET',
              success:(res)=>{
                  if(res.data.code == 0){
                      this.setData({
                          maxNum:res.data.data.maxNum,
                          tempMax:res.data.data.maxNum,
                          edit: !this.data.edit
                      })
                      wx.showToast({
                        title: '修改成功！',
                        icon:"success",
                        duration:2000
                      })
                  }else if(res.data.code == 1){
                    console.log("请求失败！")
                    wx.showToast({
                        title: '修改失败！',
                        icon:"error",
                        duration:2000
                      })
                      this.setData({
                        edit: !this.data.edit,
                        tempMax: this.data.maxNum
                    })
                  }else if(res.data.code == 14){//如果水族箱已拥有用户
                    wx.showToast({
                        title: '该水族箱已拥有用户',
                        icon:"error",
                        duration:2000
                      })
                  }
              }

            })

        },


        //重新加载当前页面
        updatePage: function () {
            console.log("页面更新！")
            var pages = getCurrentPages()    //获取加载的页面
            var currentPage = pages[pages.length - 1]    //获取当前页面的对象
            var curUrl = currentPage.route    //当前页面url
            curUrl = '../../' + curUrl
            console.log(curUrl)
            wx.reLaunch({
                url: curUrl

            })
        },

        //鱼列表详情，用一个弹出框
        detail: function () {
            wx.navigateTo({
                url: '../../pkgA/detail/detail',
            })
        },

        //打电话
        callPhone: function () {
            wx.makePhoneCall({
                phoneNumber: this.data.phone
            })
        },

        //修改负责人信息
        editCharge: function () {
            wx.navigateTo({
                url: '../../pkgA/editCharge/editCharge',
            })
        }



    },

    lifetimes: {
        created() {


        },
        attached() {
            this.setData({
                exists: wx.getStorageSync('exists'),
            })

            console.log(wx.getStorageSync('exists'))


            var aquariumId = wx.getStorageSync('userAquariumList')[wx.getStorageSync('curIndex')]

            console.log(aquariumId)
            // 发送请求获取所有鱼缸
            if (aquariumId) {


                wx.request({
                    url: `http://120.77.156.205:8808/wx/aquarium/aquariumMess/${aquariumId}`,

                    method: 'GET',
                    success: (res) => {
                        // console.log(res)
                        //成功查询到实时鱼缸的传感器数据
                        if (res.data.code == 0) {

                            this.setData({
                                aquariumMess: res.data.data.aquariumMess,

                                charge: res.data.data.aquariumMess.charge,
                                fishNum: Number(res.data.data.aquariumMess.fishNum),
                                maxNum: Number(res.data.data.aquariumMess.maxNum),
                                tempMax:Number(res.data.data.aquariumMess.maxNum),
                                gmtCreate: res.data.data.aquariumMess.gmtCreate,
                                phone: res.data.data.aquariumMess.phone,

                                value1: Number(res.data.data.aquariumMess.value1),
                                value2: Number(res.data.data.aquariumMess.value2),
                                value3: Number(res.data.data.aquariumMess.value3),
                                value4: Number(res.data.data.aquariumMess.value4),

                            })
                            // console.log(this.data.data.phone)
                            return
                        }

                        //获取实时的鱼缸传感器数据失败，获取上次最新的数据
                        if (res.data.code == 8) {
                            this.setData({
                                aquariumMess: res.data.data.aquariumMess,

                                charge: res.data.data.aquariumMess.charge,
                                fishNum: Number(res.data.data.aquariumMess.fishNum),
                                maxNum: Number(res.data.data.aquariumMess.maxNum),
                                tempMax:Number(res.data.data.aquariumMess.maxNum),

                                gmtCreate: res.data.data.aquariumMess.gmtCreate,
                                phone: res.data.data.aquariumMess.phone,

                                value1: Number(res.data.data.aquariumMess.value1),
                                value2: Number(res.data.data.aquariumMess.value2),
                                value3: Number(res.data.data.aquariumMess.value3),
                                value4: Number(res.data.data.aquariumMess.value4),

                                //用个<view>提示：当前展现的为xxx时间的数据，从硬件中获取最新数据失败
                                tip: "获取鱼缸的最新数据失败! 请检查设备是否正常"
                            })
                            return
                        }else{
                            this.setData({
                                tip:'获取鱼缸的最新数据失败! 请检查设备是否正常'
                            })
                        }
                    }


                })
            }

        },
        detached() {
           
        }
    },

    pageLifetimes: {
        show() {
        }
    }
})
